<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="车名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-input
        v-model="listQuery.brand"
        placeholder="品牌"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select v-model="listQuery.status" placeholder="状态" clearable class="filter-item" style="width: 130px">
        <el-option label="可租赁" value="可租赁" />
        <el-option label="已租出" value="已租出" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-plus" @click="handleCreate">
        新增
      </el-button>
    </div>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="加载中"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="车名" width="120">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="品牌" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.brand }}
        </template>
      </el-table-column>
      <el-table-column label="车牌号" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.plateNumber }}
        </template>
      </el-table-column>
      <el-table-column label="车型" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.type }}
        </template>
      </el-table-column>
      <el-table-column label="座位数" width="90" align="center">
        <template slot-scope="scope">
          {{ scope.row.seats }}
        </template>
      </el-table-column>
      <el-table-column label="日租金" width="100" align="center">
        <template slot-scope="scope">
          ¥{{ scope.row.dailyRate }}
        </template>
      </el-table-column>
      <el-table-column label="里程数" width="100" align="center">
        <template slot-scope="scope">
          {{ scope.row.mileage }}km
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleEdit(row)">
            编辑
          </el-button>
          <el-button 
            v-if="row.status === '可租赁'" 
            type="success" 
            size="mini" 
            @click="handleRent(row)"
          >
            租赁
          </el-button>
          <el-button 
            v-if="row.status === '已租出'" 
            type="warning" 
            size="mini" 
            @click="handleReturn(row)"
          >
            还车
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination'

export default {
  name: 'CarList',
  components: { Pagination },
  data() {
    return {
      list: [
        {
          id: 1,
          name: '奥迪A6L',
          brand: '奥迪',
          plateNumber: '京A12345',
          type: '轿车',
          seats: 5,
          dailyRate: 500,
          mileage: 30000,
          status: '可租赁'
        },
        {
          id: 2,
          name: '丰田凯美瑞',
          brand: '丰田',
          plateNumber: '京B67890',
          type: '轿车',
          seats: 5,
          dailyRate: 300,
          mileage: 45000,
          status: '已租出'
        },
        {
          id: 3,
          name: '别克GL8',
          brand: '别克',
          plateNumber: '京C13579',
          type: '商务车',
          seats: 7,
          dailyRate: 600,
          mileage: 20000,
          status: '维修中'
        }
      ],
      total: 3,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        brand: undefined,
        status: undefined
      }
    }
  },
  methods: {
    getStatusType(status) {
      const statusMap = {
        '可租赁': 'success',
        '已租出': 'warning',
        '维修中': 'danger'
      }
      return statusMap[status] || 'info'
    },
    getList() {
      this.listLoading = true
      setTimeout(() => {
        // 模拟接口调用
        let filteredList = [...this.list]
        if (this.listQuery.name) {
          filteredList = filteredList.filter(item => item.name.includes(this.listQuery.name))
        }
        if (this.listQuery.brand) {
          filteredList = filteredList.filter(item => item.brand.includes(this.listQuery.brand))
        }
        if (this.listQuery.status) {
          filteredList = filteredList.filter(item => item.status === this.listQuery.status)
        }
        this.list = filteredList
        this.total = filteredList.length
        this.listLoading = false
      }, 500)
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.$message.info('新增车辆功能开发中')
    },
    handleEdit(row) {
      this.$message.info('编辑车辆功能开发中')
    },
    handleRent(row) {
      this.$message.info('租赁功能开发中')
    },
    handleReturn(row) {
      this.$message.info('还车功能开发中')
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该车辆吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        this.$message.success('删除成功')
        this.getList()
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除失败:', error)
        }
      }
    }
  }
}
</script>

<style scoped>
.filter-container {
  padding-bottom: 10px;
}
.filter-item {
  display: inline-block;
  vertical-align: middle;
  margin-bottom: 10px;
  margin-right: 10px;
}
</style>